import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

public class GameFrame extends JFrame {
    //定义窗口大小
    final int frameWidth = 800;
    final int frameHeight = 600;

    final int step = 30;


    ArrayList<SnakeBody> snakeBodies = new ArrayList<>();
    Random random = new Random();
    FoodObj foodObj = getRandomFood();

    public FoodObj getRandomFood() {
        int lineNum = frameHeight / step;
        int x = random.nextInt(lineNum) * step;
        int y = (random.nextInt(lineNum - 1) + 1) * step;
        return new FoodObj(x, y, GameUtils.foodImg, this);
    }

    public static int score = 0;
    public static int state = 0;
    Image cacheImage = null;

    public void launch() {
        //设置窗口标题
        this.setTitle("贪吃蛇");
        //设置窗口大小
        this.setSize(frameWidth, frameHeight);
        //设置窗口相对位置
        this.setLocationRelativeTo(null);
        //设置窗口可见性
        this.setVisible(true);
        //设置窗口关闭操作  JFrame.EXIT_ON_CLOSE 关闭窗口，程序退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        snakeBodies.add(new SnakeBody(0, 570, GameUtils.bodyImg, this));
        snakeBodies.add(new SnakeBody(-30, 570, GameUtils.bodyImg, this));

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    switch (state) {
                        case 0:
                        case 2:
                            state = 1;break;
                        case 1:
                            state = 2;break;
                        case  3:
                            state = 5;
                            break;
                        case  5:
                            state = 6;
                            break;
                        default: break;
                    }
                }
            }
        });

        while (true) {
            if (state == 1) {
                repaint();
            }
            if (state == 5){
                state = 0;
                reset();
            }
            if (state == 6){
                state = 0;
                GameUtils.level++;
                GameUtils.accessScore += 10;
                reset();
            }

            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public SnakeHead snakeHead = new SnakeHead(30, 570, GameUtils.rightImg, this);

    public void painPrompt(Graphics graphics) {


        if (state == 0 || state == 2) {
            graphics.setColor(Color.gray);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "按空格键开始游戏", Color.orange, 30, 150, 285);
        }
        if (state == 3) {
            graphics.setColor(Color.gray);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "游戏失败", Color.red, 30, 150, 285);
        }
        if (state == 4) {
            graphics.setColor(Color.gray);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "游戏通关", Color.green, 30, 150, 285);
        }
    }

    public void paint(Graphics graphics) {
        if (cacheImage == null) {
            cacheImage = this.createImage(frameWidth, frameHeight);
        }
        Graphics oldGraphics = graphics;
        graphics = cacheImage.getGraphics();
        graphics.setColor(Color.DARK_GRAY);
        graphics.fillRect(0, 0, frameWidth, frameHeight);
        //设置线条颜色
        graphics.setColor(Color.gray);
        int lineNum = frameHeight / step; //划线的数量
        for (int i = 0; i <= lineNum; i++) {
            //画横线
            graphics.drawLine(0, i * step, frameHeight, i * step);
            //画竖线
            graphics.drawLine(i * step, 0, i * step, frameHeight);
        }
        for (int i = snakeBodies.size() - 1; i >= 0; i--) {
            snakeBodies.get(i).paintSelf(graphics);

        }
        snakeHead.paintSelf(graphics);
        foodObj.paintSelf(graphics);
        GameUtils.drawWord(graphics,"第"+ GameUtils.level+"关",Color.black, 50, 620, 200);
        GameUtils.drawWord(graphics, score + "分", Color.black, 50, 650, 300);
        painPrompt(graphics);
        oldGraphics.drawImage(cacheImage, 0, 0, null);
    }

    public void reset(){

        this.dispose();

        String[] args = {};
        main(args);
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }
}
